package me.learningandroid.ch2_6.fragmentdemo;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


public class InputFragment extends Fragment {

    @FunctionalInterface
    public interface AddCallback {
        void apply(ContactData data);
    }

    private AddCallback callback;

    public InputFragment() {
    }

    public void setOnAddBtnListener(AddCallback callback) {
        this.callback = callback;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.i(getClass().getName(), "onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(getClass().getName(), "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i(getClass().getName(), "onCreateView");
        // 创建`View`并创建相关的`ViewHolder`，固定写法。
        // `LayoutInflater.inflate(id, parent, attachToRoot)`的`attachToRoot`必须为false，即不在创建时与上级的视图节点关联，让`RecyclerView`来关联上级节点。
        View view = inflater.inflate(R.layout.fragment_input, container, false);
        Button addBtn = view.findViewById(R.id.addBtn);
        Button clearBtn = view.findViewById(R.id.clearBtn);
        EditText editTextName = view.findViewById(R.id.editTextName);
        EditText editTextPhone = view.findViewById(R.id.editTextPhone);
        addBtn.setOnClickListener((v) -> {
            String name = editTextName.getText().toString();
            String phone = editTextPhone.getText().toString();

            editTextName.setText("");
            editTextPhone.setText("");

            ContactData contactData = new ContactData(name, phone);
            if (callback!=null) {
                callback.apply(contactData);
            }
        });
        clearBtn.setOnClickListener((v) -> {
            editTextName.setText("");
            editTextPhone.setText("");
        });

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(getClass().getName(), "onViewCreated");
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        Log.i(getClass().getName(), "onViewStateRestored");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(getClass().getName(), "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(getClass().getName(), "onResume");
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(getClass().getName(), "onSaveInstanceState");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(getClass().getName(), "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(getClass().getName(), "onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(getClass().getName(), "onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(getClass().getName(), "onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(getClass().getName(), "onDetach");
    }

}